home comics writing pictures archive about

MainWindowVb.xaml.vb

Language: Visual Basic .NET
Last Modified: 2020-06-27 1:58:34 PM UTC
File Size: 6220 bytes
http://www.penguinstew.ca/example/randomorder/MainWindowVb.xaml.vb
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Imports System.Threading
Imports System.Windows.Threading
''' <summary>
''' Random Order program VB.net version
''' </summary>
Class MainWindow
Implements INotifyPropertyChanged
#Region "constant"
'The number of numbers to generate
Private Const COUNT As Integer = 10000
#End Region
#Region "INotifyPropertyChanged interface"
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
#End Region
#Region "private variables"
'The private field for the ProgressValue property
Private progressVar As Integer = 0
'The private field for the StatusLabel property
Private statusVar As String = "Ready"
'Bool set when program is clossing
Private abort As Boolean = False
#End Region
#Region "properties"
'The collection bound to the item source for the listBox
Public Property numbersListSource As ObservableCollection(Of String)
'The int bound to the value attribute of the progress bar
Public Property ProgressValue As Integer
Get
Return progressVar
End Get
Set(value As Integer)
progressVar = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("ProgressValue"))
End Set
End Property
'The string bound to the Content attribute of the status label
Public Property StatusContent As String
Get
Return statusVar
End Get
Set(value As String)
statusVar = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("StatusContent"))
End Set
End Property
#End Region
#Region "constructor"
''' <summary>
''' Constructor for the MainWindow class
''' </summary>
Public Sub New()
' This call is required by the designer.
InitializeComponent()
'Setup listBox binding
numbersListSource = New ObservableCollection(Of String)
numberList.ItemsSource = numbersListSource
End Sub
#End Region
#Region "event handlers"
''' <summary>
''' Called when the user clicks on the generate button
''' </summary>
''' <param name="sender">Object sending the event</param>
''' <param name="e">RoutedEventArgs for the event</param>
Private Sub generateButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
'Set status and disable button
generateButton.IsEnabled = False
StatusContent = "Generating..."
numbersListSource.Clear()
''Start bacground thread to generate numbers
Dim workerThread As Thread = New Thread(AddressOf generateNumbers)
workerThread.IsBackground = True
workerThread.Start()
End Sub
''' <summary>
''' Called when the program is clossed
''' </summary>
''' <param name="sender">Object sending the event</param>
''' <param name="e">CancelEventArgs for the event</param>
Private Sub Window_Closing(sender As System.Object, e As System.ComponentModel.CancelEventArgs)
'Set the variable to indicate to the background thread that the program is closing
abort = True
End Sub
#End Region
#Region "worker methods"
''' <summary>
''' Generates a series of numbers in random order and adds them to the listBox
''' </summary>
Private Sub generateNumbers()
Dim numbers As List(Of Integer) = New List(Of Integer)(COUNT)
Dim progressSpace As Integer = COUNT / 100
Dim progressAmmount As Integer = 1
Dim sum As Integer = 0
Dim expectedSum As Integer = COUNT * (COUNT + 1) / 2
Dim random As Random = New Random()
Dim startTime As DateTime = DateTime.Now
Dim action As Action
'Set progressAmount if progressSpace is 0
If progressSpace = 0 Then
progressSpace = 1
progressAmmount = 100 / COUNT
End If
'Populate list
For i As Integer = 0 To COUNT - 1 Step 1
numbers.Add(i + 1)
Next
For i As Integer = 0 To COUNT - 1 Step 1
'If program is aborted return so the thread can close properly
If abort Then
Return
End If
'Generate a random index
Dim index As Integer = random.Next(0, numbers.Count)
action = New Action(Sub()
numbersListSource.Add(numbers.Item(index).ToString)
End Sub)
Me.Dispatcher.Invoke(action, DispatcherPriority.ApplicationIdle)
'Add the number to the sum
sum += numbers.Item(index)
'Remove the number from the list
numbers.RemoveAt(index)
If i Mod progressSpace = 0 Then
action = New Action(Sub()
ProgressValue += progressAmmount
End Sub)
Me.Dispatcher.Invoke(action, DispatcherPriority.ApplicationIdle)
End If
Next
Dim endTime As DateTime = DateTime.Now()
'Reset the view and display a message box with the ressults
action = New Action(Sub()
ProgressValue = 0
generateButton.IsEnabled = True
StatusContent = "Done"
MessageBox.Show(String.Format("Sum of numbers = {0} " + vbCrLf + "Expected sum = {1} " _
+ vbCrLf + "Difference = {2} " _
+ vbCrLf + "Elapsed time = {3} s" _
, sum, expectedSum, expectedSum - sum, endTime.Subtract(startTime).TotalSeconds))
End Sub)
Me.Dispatcher.Invoke(action, DispatcherPriority.ApplicationIdle)
End Sub
#End Region
End Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190